package com.mrzhou.core.tool.utils;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Inet6Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

/**
 * 获取设备信息, 包括服务器网卡信息，服务器名称， IP地址
 * @author qiang.zhou
 */
@Slf4j
public class INetUtil {

    public static final String LOCAL_HOST = "127.0.0.1";

    public static final String OS_NAME = "os.name";

    public static final String LINUX = "linux";

    /**
     * 检查当前版本是否为LINUX
     * @return
     */
    public static boolean isOsLinux() {
        // 获取当前操作系统
        String osName = getOsName();
        return StringUtil.isNotBlank(osName) && osName.toLowerCase().indexOf(LINUX) > -1;
    }

    /**
     * 获取当前系统操作系统名称
     * @return
     */
    public static String getOsName() {
        return System.getProperty(OS_NAME);
    }

    /**
     * LINUX下获取服务器名称
     * @return
     */
    public static String getOsHostName() {
        try {
            if(isOsLinux()) {
                /** 获取LINUX的服务名称, {@link InetAddress}此类在linux下可能会不能正确获取 */
                Process process = Runtime.getRuntime().exec("hostname");
                BufferedReader reader = new BufferedReader(new InputStreamReader(new BufferedInputStream(process.getInputStream())));
                String line = "";
                if((line = reader.readLine()) != null) {
                    return line;
                }
                reader.close();
                process.destroy();
            }
            InetAddress address = InetAddress.getLocalHost();
            return address.getHostName();
        } catch (Exception e) {
            log.warn("获取{}的名称出错： {}", getOsName(), e);
        }
        return "";
    }

    /**
     * 获取当前操作系统不为127.0.0.1的IP地址
     * 有多张网卡时， 默认返回第一个网卡的IP地址
     * @return
     */
    public static String getOsHost() {
        try{
            if (isOsLinux()) {
                Enumeration<?> e1 = (Enumeration<?>) NetworkInterface.getNetworkInterfaces();
                while (e1.hasMoreElements()) {
                    NetworkInterface ni = (NetworkInterface) e1.nextElement();
                    if(!ni.getName().equals("eth0")) {
                        continue;
                    } else {
                        Enumeration<?> e2 = ni.getInetAddresses();
                        while (e2.hasMoreElements()) {
                            InetAddress ia = (InetAddress) e2.nextElement();
                            if (ia instanceof Inet6Address) {
                                continue;
                            }
                            return ia.getHostAddress();
                        }
                    }
                }
            }
            return InetAddress.getLocalHost().getHostAddress();
        } catch (Exception e) {
            log.warn("获取{}系统的Host出错： {}", getOsName(), e);
        }
        return LOCAL_HOST;
    }

    public static void main(String[] args) {
        System.out.println(INetUtil.getOsHost());
    }

}
